<?php

namespace App\Http\Controllers\Resource;

use Illuminate\Support\Facades\Log;

/**
 * 畅想之星 数字资源
 *
 *  提供目录接口
 */
class CxzxController extends CommonController
{

    public $appid = 'fb86855317512c46a';
    public $app_key = '6e3f8b4e-af9a-4010-a0eb-b50ee1f47e36';
    public $host_url = 'https://www.cxstar.com/CxstarLibApi';
    public $package_id = '25f8e7ac012727XXXX';

    //对接单点登录
    public $lib_id = '20f9eecb000001XXXX';
    public $lib_code = '1731';


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取所有数据
     *         book_type //枚举项：All标识全部,Ebk版式,Cxbf流式
     *         keywords_type //检索类型，//检索类型（大写），ALL标识全部，TITLE标识题名，AUTHOR标识作者，ISBN标识ISBN/ISSN，CLC标识索书号，PUBLISHER标识出版社，ABSTRACT标识摘要，SUBJECT标识主题词/关键词
     *
     *         class_id    //分类唯一号，精确检索分类唯一号下图书信息，允许为空，为空不检索分类
     *
     *         size  //每页显示数据量，枚举项： Twenty标识20，Fifty标识50，OneHundred标识100
     *
     */
    public function getResourceList($book_type = 'All', $keywords_type = 'ALL', $keywords = '', $class_id = '', $page = 1, $limit = 'Twenty')
    {
        $url = $this->host_url . '/api/Books/Center';

        $param = [
            'AppId' => $this->appid,
            'BookType' => $book_type,
            'SearchType' => $keywords_type,
            'Keyword' => $keywords,
            'ClassId' => $class_id,
            'Page' => $page,
            'Size' => $limit
        ];

        $sign = $this->getSign($param);
        $param['Sign'] = $sign;
        $result = request_url($url, json_encode($param), 60, true);
        $result = json_decode($result, true);

       // Log::channel('cxzxlog')->debug($param);

        if ((empty($result['Code']) || $result['Code'] !== "1") || empty($result['Data'])) {
            Log::channel('cxzxlog')->debug('获取所有资源失败');
            Log::channel('cxzxlog')->debug($param);
            Log::channel('cxzxlog')->debug($result);
            Log::channel('cxzxlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }


    /**
     *获取资源分类
     */
    public function getResourceType()
    {
        $url = $this->host_url . '/api/Package/Categories';

        $param = [
            'AppId' => $this->appid,
            'PackageId' => $this->package_id
        ];

        $sign = $this->getSign($param);
        $param['Sign'] = $sign;
        $result = request_url($url, json_encode($param), 10, true);
        $result = json_decode($result, true);

        if (empty($result['Code']) || $result['Code'] !== "1") {
            Log::channel('cxzxlog')->debug('获取资源分类失败');
            Log::channel('cxzxlog')->debug($param);
            Log::channel('cxzxlog')->debug($result);
            Log::channel('cxzxlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }

    /**
     * 获取增量数据
     *         book_type //枚举项：All标识全部,Ebk版式,Cxbf流式
     *
     *         size  //每页显示数据量，枚举项： Twenty标识20，Fifty标识50，OneHundred标识100
     *
     */
    public function getResourceIncreaseList($book_type = 'All', $start_time = '', $end_time = '', $page = 1, $limit = 'Twenty')
    {
        $url = $this->host_url . '/api/Books/Center/Add';

        $param = [
            'AppId' => $this->appid,
            'BookType' => $book_type,
            'LastUpdateTime' => $start_time, //上一次更新时间
            'StartUpdateTime' => $end_time, //本次开始更新时间
            'Page' => $page,
            'Size' => $limit
        ];

        $sign = $this->getSign($param);
        $param['Sign'] = $sign;
        $result = request_url($url, json_encode($param), 10, true);
        $result = json_decode($result, true);

        if ((empty($result['Code']) || $result['Code'] !== "1") || empty($result['Data'])) {
            Log::channel('cxzxlog')->debug('获取增量资源失败');
            Log::channel('cxzxlog')->debug($param);
            Log::channel('cxzxlog')->debug($result);
            Log::channel('cxzxlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }

    /**
     * 获取下架数据
     *         book_type //枚举项：All标识全部,Ebk版式,Cxbf流式
     *
     *         size  //每页显示数据量，枚举项： Twenty标识20，Fifty标识50，OneHundred标识100
     *
     */
    public function downResourceList($book_type = 'All', $start_time = '', $end_time = '', $page = 1, $limit = 'Twenty')
    {
        $url = $this->host_url . '/api/Books/Center/Remove';

        $param = [
            'AppId' => $this->appid,
            'BookType' => $book_type,
            'LastUpdateTime' => $start_time, //上一次更新时间
            'StartUpdateTime' => $end_time, //本次开始更新时间
            'Page' => $page,
            'Size' => $limit
        ];

        $sign = $this->getSign($param);
        $param['Sign'] = $sign;
        $result = request_url($url, json_encode($param), 10, true);
        $result = json_decode($result, true);

        if ((empty($result['Code']) || $result['Code'] !== "1") || empty($result['Data'])) {
            Log::channel('cxzxlog')->debug('获取下架资源失败');
            Log::channel('cxzxlog')->debug($param);
            Log::channel('cxzxlog')->debug($result);
            Log::channel('cxzxlog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }

    /**
     * 获取签名
     * @param $data
     */
    public function getSign($param = [])
    {
        $param = $this->arraySort($param);
        $params = http_build_query($param); //null 不参与拼接，但是 空字符串要参与拼接
        $sign = strtoupper(md5(urldecode($params . '&key=' . $this->app_key))); //签名中文不转码，传输中文需要转码  urldecode 中文按正常输出
        return $sign;
    }


    /**
     * 获取畅想之星跳转地址
     * @param $url  跳转地址
     * @param $user_info  用户信息  (需要开发一个接口，验证读者证号是否有效，只要这里有效即可)
     * @return string
     */
    public function getSkipUrl($url, $user_info)
    {
        $lib_id = $this->lib_id;
        $lib_code = $this->lib_code;
        $account = $user_info['tel'];
        $username = $user_info['nickname'];
        $nonce = uniqid(); //随机数
        $key = md5($account . '_' . $lib_id  . '_' .  $lib_code  . '_' .  $nonce);
        $url = urlencode($url);
        $url = 'https://ua.cxstar.com/uniauth/dxValLogin/las2CXStar?LibID=' . $lib_id . '&id=' . $account . '&name=' . $username . '&nonce=' . $nonce . '&key=' . $key . '&returnurl=' . $url;

        return $url;
    }



    /**
     * 字典序排序
     */
    public function arraySort(array $param)
    {
        ksort($param);
        reset($param);

        return $param;
    }
}
